package com.git.hui.yinshua.biz.rest.admin.goods;

import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.goods.service.GoodsService;
import com.git.hui.yinshua.biz.rest.models.goods.req.GoodsQueryReq;
import com.git.hui.yinshua.biz.rest.models.goods.req.OperateGoodsReq;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.GoodsInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品管理
 *
 * @author YiHui
 * @date 2024/6/13
 */
@RequestMapping(path = "admin/goods")
@RestController
@Permission(role = UserAccessRole.ADMIN)
public class AdminGoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 添加商品
     *
     * @return
     */
    @PostMapping(path = "add")
    public ResVo<Boolean> addItem(@RequestBody OperateGoodsReq req) {
        // fixme 参数校验
        if (req.getPrice() == null) {
            // 商品没有价格，则从spec中取价格
            req.setPrice(req.getSpecList().get(0).getPrice());
            req.setDiscountPrice(req.getSpecList().get(0).getDiscountPrice());
        }

        goodsService.add(req);
        return ResVo.ok(true);
    }

    /**
     * 添加商品
     *
     * @return
     */
    @PostMapping(path = "update")
    public ResVo<Boolean> updateItem(@RequestBody OperateGoodsReq req) {
        // fixme 参数校验
        if (req.getGoodsId() == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "goodsId不能为空!");
        }

        goodsService.update(req);
        return ResVo.ok(true);
    }


    @RequestMapping(path = "list")
    public ResVo<ListRsp<GoodsInfoDto>> listGoods(GoodsQueryReq query) {
        query.autoInitReq();
        ListRsp<GoodsInfoDto> list = goodsService.queryFullGoodsList(query);
        return ResVo.ok(list);
    }

    /**
     * 上下架
     *
     * @param goodsId
     * @param online
     * @return
     */
    @RequestMapping(path = "online")
    public ResVo<Boolean> online(@RequestParam("goodsId") Integer goodsId, @RequestParam("online") Boolean online) {
        return ResVo.ok(goodsService.online(goodsId, online));
    }

    /**
     * 推荐
     *
     * @param goodsId
     * @param recommend
     * @return
     */
    @RequestMapping(path = "recommend")
    public ResVo<Boolean> recommend(@RequestParam("goodsId") Integer goodsId, @RequestParam("recommend") Boolean recommend) {
        return ResVo.ok(goodsService.recommend(goodsId, recommend));
    }

    /**
     * 删除
     *
     * @param goodsId
     * @return
     */
    @RequestMapping(path = "deleted")
    public ResVo<Boolean> deleted(@RequestParam("goodsId") Integer goodsId) {
        return ResVo.ok(goodsService.deleted(goodsId));
    }
}
